package com.sgb.packers.widget;

import android.content.Context;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.sgb.packers.R;
import com.sgb.packers.view.adapter.SelectAdapter;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.interfaces.DialogClick;
import com.swgk.core.util.PickSelectEntity;

import java.util.List;

/**
 * @author : younghow
 * @date : 2021/11/29 16:02
 * description:
 */
public class SelectPopWindow extends BasePopWindow implements View.OnClickListener {

    private DialogClick<PickSelectEntity> mDialogClick;
    private SelectAdapter timeSalaryAdapter;

    public void setDialogClick(DialogClick<PickSelectEntity> mDialogClick) {
        this.mDialogClick = mDialogClick;
    }

    public SelectPopWindow(Context context) {
        super(context, DIR_DOWN_UP);
    }

    @Override
    protected int popLayout() {
        return R.layout.packers_pop_project_unit;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        TextView tvPopSure = holder.getPop().findViewById(R.id.packers_tv_pop_sure);
        TextView tvPopCancel = holder.getPop().findViewById(R.id.packers_tv_pop_cancel);
        RecyclerView rvTimeSalary = holder.getPop().findViewById(R.id.packers_rv_unit);
        tvPopCancel.setOnClickListener(this);
        tvPopSure.setOnClickListener(this);
        this.setHeight(WindowManager.LayoutParams.MATCH_PARENT);
        this.setWidth(WindowManager.LayoutParams.MATCH_PARENT);
        this.setClippingEnabled(false);
        rvTimeSalary.setLayoutManager(new GridLayoutManager(mContext, 4));
        timeSalaryAdapter = new SelectAdapter(mContext, null);
        timeSalaryAdapter.setItemSelect(true);
        rvTimeSalary.setAdapter(timeSalaryAdapter);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.packers_tv_pop_sure) {
            dismiss();
            if (timeSalaryAdapter.getCheckDatas().size() > 0) {
                mDialogClick.confirmClickData(timeSalaryAdapter.getCheckDatas().get(0));
            } else {
                mDialogClick.confirmClickData(null);
            }
        } else if (v.getId() == R.id.packers_tv_pop_cancel) {
            mDialogClick.cancelClick();
            dismiss();
        }
    }

    public void setData(List<PickSelectEntity> list) {
        timeSalaryAdapter.setDatas(list);
        timeSalaryAdapter.notifyDataSetChanged();
    }

    public void setDefaultUnit(int position){
        timeSalaryAdapter.setDefItem(position);
    }
}
